package com.c.ctgproject.adapter.guang

import com.c.ctgproject.R
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.databinding.ItemCreatePassCheckListBinding
import com.c.ctgproject.entity.guang.CheckPassDetailBean
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.showToast
import com.c.ctgproject.utils.textSet

class CreatePassCheckAdapter(private val context: Context, private val mList: ArrayList<CheckPassDetailBean>, private val listener: CreatePassCheckAdapterListener) : RecyclerView.Adapter<CreatePassCheckAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = ItemCreatePassCheckListBinding.inflate(LayoutInflater.from(context), parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val data = mList[position]
        //赋值
        holder.count.textSet(data.count.handlerNull())
        holder.remark.textSet(data.remark.handlerNull())

        holder.partName2ET.setText(data.name2.handlerNull())

        val unit = if (data.unit.handlerNull().isEmpty()) "请选择物品单位" else data.unit.handlerNull()
        holder.mPartUnitTV.text = unit
        val type = if (data.type.handlerNull().isEmpty()) "请选择物品类型" else data.type.handlerNull()
        holder.mPartTypeTV.text = type
        val name = if (data.name.handlerNull().isEmpty()) "请选择物品名称" else data.name.handlerNull()
        holder.mPartNameTV.text = name


        //删除
        holder.delete.setOnClickListener {
            if (mList.size > 1) {
                mList.removeAt(holder.adapterPosition)
                notifyItemRemoved(holder.adapterPosition)
                listener.onCountChange()
            } else {
                context.showToast("至少保留一条明细")
            }
        }
        holder.mPartTypeLL.setOnClickListener {
            listener.onPartTypeNeedSelect(holder.adapterPosition)
        }
        holder.mPartNameLL.setOnClickListener {
            listener.onPartNameNeedSelect(holder.adapterPosition)
        }
        holder.mPartUnitLL.setOnClickListener {
            listener.onPartUnitNeedSelect(holder.adapterPosition)
        }

        //输入框监听
        val watcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                when {
                    holder.partName2ET.hasFocus() -> {
                        data.name2 = holder.partName2ET.text.handlerNull()
                    }
                    holder.count.hasFocus() -> {
                        val count=if (holder.count.text.handlerNull().isNotEmpty()) holder.count.text.handlerNull().toInt() else null
                        data.count = count
                        listener.onCountChange()
                    }
                    holder.remark.hasFocus() -> {
                        data.remark = holder.remark.text.handlerNull()
                    }
                }
            }
        }
        holder.partName2ET.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                holder.partName2ET.addTextChangedListener(watcher)
            } else {
                holder.partName2ET.removeTextChangedListener(watcher)
            }
        }
        holder.count.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                holder.count.addTextChangedListener(watcher)
            } else {
                holder.count.removeTextChangedListener(watcher)
            }
        }
        holder.remark.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                holder.remark.addTextChangedListener(watcher)
            } else {
                holder.remark.removeTextChangedListener(watcher)
            }
        }
        //展不展示删除
        holder.delete.visibility = if (mList.size == 1) View.GONE else View.VISIBLE
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    inner class MyViewHolder(view: ItemCreatePassCheckListBinding) : RecyclerView.ViewHolder(view.root) {
        val delete = view.delete
        val mPartTypeLL = view.mPartTypeLL
        val mPartTypeTV = view.mPartTypeTV
        val mPartNameLL = view.mPartNameLL
        val mPartNameTV = view.mPartNameTV
        val partName2ET = view.partName2ET
        val mPartUnitLL = view.mPartUnitLL
        val mPartUnitTV = view.mPartUnitTV
        val count = view.count
        val remark = view.remark
    }

    interface CreatePassCheckAdapterListener {
        /**物品名称需要选择*/
        fun onPartNameNeedSelect(position: Int)

        /**物品类型需要选择*/
        fun onPartTypeNeedSelect(position: Int)

        /**物品单位需要选择*/
        fun onPartUnitNeedSelect(position: Int)
        fun onCountChange()

    }
}

